package com.yz.xyt.controller;

import com.yz.xyt.constant.Constants;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.LeaveRule;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.service.ILeaveRuleService;
import com.yz.xyt.service.ILeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报备请假规则管控
 * @author JW.WANG
 */
@RestController
@RequestMapping("/leaveRule")
public class LeaveRuleController {
    @Autowired
    ILeaveRuleService leaveRuleService;

    @Autowired
    ILeaveService leaveService;

    /**
     * 获取规则列表
     * @return
     */
    @RequestMapping("/getRuleList")
    public ResponseResult getRuleList(){
        List<LeaveRule> ruleList = leaveRuleService.findForList();
        ruleList.stream().forEach(rule->{
            String status = rule.getStatus();
            if(Constants.REPORT_LEAVE_STOP.equals(status)){
                rule.setStatusStr(Constants.TRANSLATE_REPORT_LEAVE_STOP);
            }
            if(Constants.REPORT_LEAVE_START.equals(status)){
                rule.setStatusStr(Constants.TRANSLATE_REPORT_LEAVE_START);
            }
        });
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("获取规则列表成功");
        Map<String,Object> map = new HashMap<>();
        map.put("list",ruleList);
        result.setData(map);
        return result;
    }

    /**
     * 启用报备请假规则
     * @param ruleId 规则id
     * @return
     */
    @RequestMapping("/startRule")
    public ResponseResult startRule(Integer ruleId){
        ResponseResult result = new ResponseResult();
        leaveService.startLeaveRule(ruleId);
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("规则启用成功");
        return result;
    }
    /**
     * 停用报备请假规则
     * @param ruleId 规则id
     * @return
     */
    @RequestMapping("/stopRule")
    public ResponseResult stopRule(Integer ruleId){
        ResponseResult result = new ResponseResult();
        leaveService.stopLeaveRule(ruleId);
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("规则停用成功");
        return result;
    }
}
